#The following example, downloads stock data from Yahoo and plots it.
import datetime

from matplotlib import ticker
from pandas.io.data import get_data_yahoo
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib.finance import candlestick_ohlc, candlestick2_ohlc

symbol = "GOOG"

#data = get_data_yahoo(symbol, start = '2013-9-01', end = '2013-10-23')[['Open','Close','High','Low','Volume']]
#data.to_csv('test.csv')
data = pd.read_csv('test.csv')
# print(data)

#ax = subplots()
fig, ax = plt.subplots()
print(type(fig), type(ax))
print(fig, ax)
# print(type(data['Open']))
# print(data['Open'].values)
candlestick2_ohlc(ax, data['Open'].values,data['High'].values,data['Low'].values,data['Close'].values, width=0.6)

# print(type(data['Date']))
# for item in data['Date']:
#     print(item)
#     print(type(item))
#     print(datetime.datetime.strptime(item,'%Y-%m-%d'))

# xdate = [datetime.datetime.fromtimestamp(i) for i in data['Date']]
xdate = [str(i) for i in data['Date']]
print(xdate)
ax.xaxis.set_major_locator(ticker.MaxNLocator(30))

def mydate(x,pos=None):
    try:
        return xdate[int(x)]
        #return xdate[int(x)].strftime('%Y-%m-%d')
    except IndexError:
        return ''

ax.xaxis.set_major_formatter(ticker.FuncFormatter(mydate))

fig.autofmt_xdate()
fig.tight_layout()

plt.show()